home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / rng / borosh13.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-02  |  2.1 KB  |  92 lines

  1. /* rng/borosh13.c
  2.  * 
  3.  * This program is free software; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or (at
  6.  * your option) any later version.
  7.  * 
  8.  * This program is distributed in the hope that it will be useful, but
  9.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.  * General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /*
  19.  * This generator is taken from
  20.  *
  21.  * Donald E. Knuth
  22.  * The Art of Computer Programming
  23.  * Volume 2
  24.  * Third Edition
  25.  * Addison-Wesley
  26.  * Page 106-108
  27.  *
  28.  * It is called "Borosh - Niederreiter"
  29.  *
  30.  * This implementation copyright (C) 2001 Carlo Perassi.
  31.  */
  32.  
  33. #include <config.h>
  34. #include <stdlib.h>
  35. #include <gsl/gsl_rng.h>
  36.  
  37. #define AA 1812433253UL
  38. #define MM 0xffffffffUL        /* 2 ^ 32 - 1 */
  39.  
  40. static inline unsigned long int ran_get (void *vstate);
  41. static double ran_get_double (void *vstate);
  42. static void ran_set (void *state, unsigned long int s);
  43.  
  44. typedef struct
  45. {
  46.   unsigned long int x;
  47. }
  48. ran_state_t;
  49.  
  50. static inline unsigned long int
  51. ran_get (void *vstate)
  52. {
  53.   ran_state_t *state = (ran_state_t *) vstate;
  54.  
  55.   state->x = (AA * state->x) & MM;
  56.  
  57.   return state->x;
  58. }
  59.  
  60. static double
  61. ran_get_double (void *vstate)
  62. {
  63.   ran_state_t *state = (ran_state_t *) vstate;
  64.  
  65.   return ran_get (state) / 4294967296.0;
  66. }
  67.  
  68. static void
  69. ran_set (void *vstate, unsigned long int s)
  70. {
  71.   ran_state_t *state = (ran_state_t *) vstate;
  72.  
  73.   if (s == 0)
  74.     s = 1;            /* default seed is 1 */
  75.  
  76.   state->x = s & MM;
  77.  
  78.   return;
  79. }
  80.  
  81. static const gsl_rng_type ran_type = {
  82.   "borosh13",            /* name */
  83.   MM,                /* RAND_MAX */
  84.   0,                /* RAND_MIN */
  85.   sizeof (ran_state_t),
  86.   &ran_set,
  87.   &ran_get,
  88.   &ran_get_double
  89. };
  90.  
  91. const gsl_rng_type *gsl_rng_borosh13 = &ran_type;
  92.